import 'dart:async';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/http/apis/common.dart';
import 'package:qgchat/http/models/newanchorModel.dart';

class NewManBloc implements BlocBase {

  NewManBloc() {
    onRefresh();
  }

  /// 分页
  int _page = 1;

  /// 分页数
  int _limit = 6;

  /// 热门主播列表
  List<NewanchorModel> newlist = [];

  // ##########  STREAMS  ##############

  /// 主播新人stream
  StreamController<List<NewanchorModel>> _newanchorController = StreamController.broadcast();
  Sink<List<NewanchorModel>> get _inNewanchorSink => _newanchorController.sink;
  Stream<List<NewanchorModel>> get outNewanchorStream => _newanchorController.stream;

  void dispose(){
    _newanchorController.close();
  }

  // ##########  HANDLING  ##############

  /// 获取新人主播列表数据
  Future getNewanchorList() async {
    List<NewanchorModel> result = await CommonApis.newanchor(page: _page, limit: _limit);
    newlist.addAll(result);
    if (!_newanchorController.isClosed) {
      _inNewanchorSink.add(newlist);
    }
  }

    /// 重新加载页面
  Future onRefresh() async {
    _page = 1;
    newlist.clear();

    await getNewanchorList();
  }

  /// 加载更多
  Future onLoad() async {
    _page++;
    getNewanchorList();
  }

}